from tools import create_sprite_dict
from settings import *


class BaseActive(object):
    def __init__(self, *images, x=0, y=0):
        self._name = None
        self._direction = PG.math.Vector2()
        self._direction.x = x
        self._direction.y = y
        self.images = []
        self.images_nums = images
        self.images_length = len(self.images_nums)

    def set_name(self, name):
        self._name = name

    @property
    def name(self):
        return self._name

    @property
    def direction(self):
        return self._direction


class Inactive(BaseActive):
    active = False


class Active(BaseActive):
    active = True


def get_id_generator(cls):
    while True:
        yield cls.instance_id
        cls.instance_id += 1


class SpriteMeta(type):
    def __new__(mcs, cls_name, cls_parent, cls_dict):
        all_images = []
        set_default = True
        default_active = None
        for key, val in cls_dict.items():
            if key.startswith("images_"):
                all_images.extend(create_sprite_dict(val))
                continue
            if isinstance(val, BaseActive):
                val.set_name(key)
                for i in val.images_nums:
                    val.images.append(all_images[i])
                if set_default:
                    set_default = False
                    default_active = val
        cls_dict["default_active"] = default_active
        cls_dict["instance_id"] = 0
        obj = super().__new__(mcs, cls_name, cls_parent, cls_dict)
        obj.gen_id = get_id_generator(obj)
        return obj
